package com.zcf.controller;

import java.util.List;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.pojo.Log;
import com.zcf.pojo.Orderinfo;
import com.zcf.pojo.Riding;
import com.zcf.pojo.RidingOrder;
import com.zcf.pojo.User;
import com.zcf.utils.DateUtil;
import com.zcf.utils.Hutool;
import com.zcf.utils.zhifubao.Query;

@Controller
@RequestMapping("/Scheduled")
@CrossOrigin
@ResponseBody
public class ScheduledCotroller {
	/**
	 * 后台强制退款
	 * 
	 * @throws Exception
	 */
	@PostMapping("qiangzhituikuan")
	public Body qiangzhituikuan(String orderInfoId) throws Exception {
		Orderinfo orderinfo = new Orderinfo();
		orderinfo.setOrderInfoId(orderInfoId);
		orderinfo = orderinfo.selectById();
		if (orderinfo != null) {
			if (orderinfo.getStatus() == 0 || orderinfo.getStatus() == 3 || orderinfo.getStatus() == 4
					|| orderinfo.getStatus() == 5 || orderinfo.getDinStatus() == 3) {
				return Body.BODY_400;
			}
			Boolean flag = false;
			if (orderinfo.getPayment().equals("1")) {
				Double money = 0.0;
				if (orderinfo.getCouponMoney() != null) {
					money = orderinfo.getTotalMoney() - Double.parseDouble(orderinfo.getCouponMoney());
				}
				String prices = money + "";
				Integer count = AliAndWXRefund.wxRefund(money, orderinfo.getOrderNumber());
				if (count == 1) {
					Orderinfo orderinfo2 = new Orderinfo();
					orderinfo2.setStatus(4);
					orderinfo2.setDinStatus(6);// 退款
					orderinfo2.setOrderInfoId(orderinfo.getOrderInfoId());
					orderinfo2.updateById();

					User user = new User();
					user.setId(orderinfo.getUserId());
					user.selectById();
					Log log = new Log();
					log.setLogId(Hutool.getId());
					log.setAddtime(Hutool.parseDateToString());
					log.setMoneys(money + "");
					log.setLogDesc("微信退款");
					log.setLogType(1);
					log.setLogName(user.getName() + ":微信退款" + money + "元");
					log.insert();
					return Body.newInstance("退款成功");
				}
				return Body.newInstance("退款失败");
			} else {
				Double money = 0.0;
				if (orderinfo.getCouponMoney() != null) {
					money = orderinfo.getTotalMoney() - Double.parseDouble(orderinfo.getCouponMoney());
				}
				flag = Query.refundMoney(orderinfo.getOrderNumber(), money);
				if (flag) {
					Orderinfo orderinfo2 = new Orderinfo();
					orderinfo2.setStatus(4);
					orderinfo2.setDinStatus(6);// 退款
					orderinfo2.setOrderInfoId(orderinfo.getOrderInfoId());
					orderinfo2.updateById();

					User user = new User();
					user.setId(orderinfo.getUserId());
					user.selectById();
					if (user != null) {
						Log log = new Log();
						log.setLogId(Hutool.getId());
						log.setAddtime(Hutool.parseDateToString());
						log.setMoneys(money + "");
						log.setLogDesc("ZFBTK");
						log.setLogType(1);
						log.setLogName(user.getName() + ":支付宝退款" + money + "元");
						log.insert();
					}
					return Body.newInstance("退款成功");
				}
				return Body.newInstance("退款失败");
			}
		}

		return Body.BODY_451;
	}

//	/**
//	 * 每天凌晨触发任务
//	 */
//	@Scheduled(cron = "0 0 0 * * ?")
//	public void clearDate() throws Exception {
//		MyCouponController mm = new MyCouponController();
//		/**
//		 * 用户清除过期优惠劵
//		 */
//		mm.deleteMy();
//		/**
//		 * 清除过期优惠劵
//		 */
//		mm.deleteDin();
//	}

//	/**
//	 * 半小时运行一次 0 0/30 0 * * ? 每5分钟执一次
//	 */
//	@Scheduled(cron = "0 */5 * * * ?")
//	public void clear() {
//		/**
//		 * 骑手超时提醒
//		 */
//		this.chaoshi();
//		/**
//		 * 订单完成 默认收货
//		 */
//		this.wancheng();
//	}

	/**
	 * 订单完成 默认收货
	 */
	public void wancheng() {
		EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
		ew.eq("din_status", 2);
		ew.eq("status", 1);
		Orderinfo orderinfo = new Orderinfo();
		List<Orderinfo> list = orderinfo.selectAll();
		String times = Hutool.parseDateToString();
		for (Orderinfo orderinfo2 : list) {
			String sum = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "m");
			String num = DateUtil.dateDiff(orderinfo2.getAddtime(), times, "yyyy-MM-dd HH:mm:ss", "h");
			int a = Integer.parseInt(sum);
			int b = Integer.parseInt(num);
			if (a > 30 || b > 1) {
				orderinfo2.setStatus(3);
				orderinfo2.setDinStatus(2);
				Boolean falg = orderinfo2.updateById();
			}
		}
	}

	/**
	 * 骑手超时提醒
	 */
	public void chaoshi() {
		Orderinfo orders = new Orderinfo();
		EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
		ew.eq("din_status", 2);
		List<Orderinfo> list = orders.selectList(ew);
		for (Orderinfo orderinfo : list) {
			String ptime = orderinfo.getPredictTime();
			String mtime = Hutool.parseDateToString();
			String times = DateUtil.dateDiff(ptime, mtime, "yyyy-MM-dd HH:mm:ss", "m");
			String timesa = DateUtil.dateDiff(ptime, mtime, "yyyy-MM-dd HH:mm:ss", "h");
			if (Integer.parseInt(timesa) < 0) {
				RidingOrder ridingOrder = new RidingOrder();
				EntityWrapper<RidingOrder> ews = new EntityWrapper<>();
				ews.eq("order_id", orderinfo.getOrderInfoId());
				ridingOrder.delete(ews);
			}
			if (Integer.parseInt(times) < 0) {
				RidingOrder ridingOrder = new RidingOrder();
				EntityWrapper<RidingOrder> ews = new EntityWrapper<>();
				ews.eq("order_id", orderinfo.getOrderInfoId());
				ridingOrder = ridingOrder.selectOne(ews);

				Riding riding = new Riding();
				riding.setId(ridingOrder.getRidingId());
				riding = riding.selectById();

				Log log = new Log();
				log.setLogId(Hutool.getId());
				log.setLogDesc("超时提醒");
				log.setLogName("超时提醒");
				log.setAddtime(Hutool.parseDateToStringMM());
				log.setUserId(ridingOrder.getRidingId());
				log.insert();
			}
		}
	}
}
